import { useClipboard } from "@vueuse/core";
import { Toast } from "vant";
import { watch } from "vue";

export const useCopy = () => {
   /*
   * 1. copy(需要拷贝的内容)
   *  2. copied 是否拷贝成功，默认1.5s恢复状态
   *  3. isSupported 浏览器是否支持，需要授权读取粘贴板和写入粘贴板权限
   * */
   const { copy, copied, isSupported } = useClipboard();

   const onCopy = async (copyText: string) => {
      if (!isSupported) return Toast.fail("系统不支持复制");
      await copy(copyText);
   };

   watch(copied, () => {
      if (copied.value) Toast.success("复制成功");
   });

   return {
      onCopy,
   };
};
